package com.smooth.site.sx.mag.biz.compcollege.comp.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Lists;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.dao.gen.BizCompCollegeMapper;
import com.smooth.dao.gen.bean.BaseCategory;
import com.smooth.dao.gen.bean.BaseCollege;
import com.smooth.dao.gen.bean.BizCompCollegeExample;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.site.sx.mag.basedata.category.enums.CategoryType;
import com.smooth.site.sx.mag.basedata.category.service.BaseBizCategoryService;
import com.smooth.site.sx.mag.basedata.category.service.BaseCategoryService;
import com.smooth.site.sx.mag.basedata.college.service.BaseCollegeService;
import com.smooth.site.sx.mag.biz.comp.service.BizCompanyService;
import com.smooth.site.sx.mag.biz.compcollege.comp.bean.CompCollegeBean;
import com.smooth.site.sx.mag.biz.compcollege.comp.enums.CompTypeEnums;
import com.smooth.site.sx.mag.biz.compcollege.comp.service.CompCollegeService;
import com.smooth.site.sx.mag.biz.compcollege.comp.service.CompCollegeTreeService;
@Controller
@RequestMapping(value = "${adminPath}/compCollege/comp")
public class BizCompSchoolController {
	private final static Logger log = LoggerFactory.getLogger(BizCompSchoolController.class);
	@Autowired
	BaseCategoryService baseCategoryService;
	@Autowired
	private BizCompanyService bizCompanyService;
	@Autowired
	BaseBizCategoryService baseBizCategoryService;
	@Autowired
	CompCollegeService compCollegeService;
	@Autowired
	BaseCollegeService baseCollegeService;
	@Autowired
	CompCollegeTreeService compCollegeTreeService;
	@RequiresPermissions("comp:bizCompany:view")
	@RequestMapping(value = "")
	public String index() {
		return "compCollege/comp/compIndex";
	}
	
	
	@RequestMapping(value = "compCateList")
	public String compTree(Model model) {
		Map<String,List<BizCompany>> dmap = compCollegeService.queryTreeCompByCategory();
		model.addAttribute("dmap", dmap);
		
		List<BaseCategory> dlist = baseBizCategoryService.queryCategory(CategoryType.company);
		
		
		List<BaseCategory> dlistEnd = baseBizCategoryService.removeNullCategory(dlist, dmap);
		model.addAttribute("categoryList", dlistEnd);
		return "compCollege/comp/compTree";
	}
	
	@RequestMapping(value = {"search"})
	public String list(BaseCollege baseCollege,String companyId, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			//查询所在省的所有学校
			List<CompCollegeBean> rlist = compCollegeTreeService.queryCompTree(baseCollege, companyId, CompTypeEnums.compCollege);
			model.addAttribute("rlist", rlist);
			model.addAttribute("companyId", companyId);
			model.addAttribute("baseCollege", baseCollege);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "compCollege/comp/collegeTreeList";
		}
		return "compCollege/comp/collegeTreeList";
	}
	@Autowired
	BizCompCollegeMapper bizCompCollegeMapper;
	@RequestMapping(value = {"save"})
	public String list(BaseCollege baseCollege,String[] compChecked, @RequestParam("compIds") String[] compIds,HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			List<String> collegeIdList = new ArrayList<String>();
			if(compChecked!=null && compChecked.length>0) {
				collegeIdList = Lists.newArrayList(compChecked);
			}
			collegeIdList.removeAll(Collections.singleton("")); 
			List<String> compIdList = Lists.newArrayList(compIds);
			compIdList.removeAll(Collections.singleton("")); 
			
			
			if(compIds!=null && compIds.length>0) {
				BizCompCollegeExample bizCompCollegeExample = new BizCompCollegeExample();
				bizCompCollegeExample.createCriteria().andCompanyIdIn(Lists.asList(new String(), compIds));
				int delnum = bizCompCollegeMapper.deleteByExample(bizCompCollegeExample);
				log.info(delnum+"删除老的关系映射");
			}
			
			compCollegeService.saveCompCollege(collegeIdList,compIdList,CompTypeEnums.compCollege);
			log.info("关系建立完毕，开始构建前端显示树");
			List<CompCollegeBean> rlist =  compCollegeTreeService.queryCompTree(baseCollege,compIdList.get(0),CompTypeEnums.compCollege); 
			log.info("构建前端显示树完成");
			model.addAttribute("rlist", rlist);
			model.addAttribute("baseCollege", new BaseCollege());
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "compCollege/comp/collegeTreeList";
		}
		return "compCollege/comp/collegeTreeList";
	}

	@RequestMapping(value = "none")
	public String none() {
		return "compCollege/comp/compNone";
	}
}
